package com.example.abilitydemo.slice;

import com.example.abilitydemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.bundle.ElementName;
import ohos.distributedhardware.devicemanager.DeviceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

public class MainAbilitySlice extends AbilitySlice {
    public static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP, 0x11101, "MainAbility");


    // 创建连接回调实例
    private IAbilityConnection connection = new IAbilityConnection() {
        // 连接到 Service 的回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int
                resultCode) {
            HiLog.info(loglabel, "MainAbilitySlice onAbilityConnectDone start ....");
            // Client 侧需要定义与 Service 侧相同的 IRemoteObject 实现类。开发者获取服务端传过来IRemoteObject 对象，并从中解析出服务端传过来的信息。
        }

        // 断开与 Service 连接的回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
            HiLog.info(loglabel, "MainAbilitySlice onAbilityDisconnectDone start ....");
        }
    };

    @Override
    public void onStart(Intent intent) {
        HiLog.info(loglabel, "MainAbilitySlice onStart start ....");
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //点击进入foo页面
        Text text = (Text) findComponentById(ResourceTable.Id_text_foopage);
        text.setClickedListener(listener -> present(new FooAbilitySlice(), new Intent()));

        startLocalServiceAbility();

//        Text textStart = (Text) findComponentById(ResourceTable.Id_text_start);
//        textStart.setClickedListener(listener -> {
//            startLocalServiceAbility();
//            HiLog.info(loglabel, "启动本地serviceAbility ....");
//        });

        Text textStop = (Text) findComponentById(ResourceTable.Id_text_stop);
        if (textStop != null) {
            textStop.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    stopLocalServiceAbility();
                    HiLog.info(loglabel, "停止本地serviceAbility ....");
                }
            });
        }

        Text textConnection = (Text) findComponentById(ResourceTable.Id_text_connection);
        if (textConnection != null) {
            textConnection.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    connectLocalServerAbility();
                    HiLog.info(loglabel, "连接serviceAbility ....");
                }
            });
        }
    }

    @Override
    public void onActive() {
        HiLog.info(loglabel, "MainAbilitySlice onActive start ....");
        super.onActive();
    }

    @Override
    public void onInactive() {
        HiLog.info(loglabel, "MainAbilitySlice onInactive start ....");
        super.onInactive();
    }

    @Override
    public void onBackground() {
        HiLog.info(loglabel, "MainAbilitySlice onBackground start ....");
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        HiLog.info(loglabel, "MainAbilitySlice onForeground start ....");
        super.onForeground(intent);
    }

    @Override
    public void onStop() {
        HiLog.info(loglabel, "MainAbilitySlice onStop start ....");
        super.onStop();
    }

    /**
     * 启动本地服务 Ability
     */
    public void startLocalServiceAbility() {
        Intent intent = new Intent();

        //构建操作方式
        Operation operation = new Intent.OperationBuilder()
                // 设备 id本地设备
                .withDeviceId("")
                // 应用的包名
                .withBundleName("com.example.abilitydemo")
                // 跳转目标的路径名 通常是包名+类名
                .withAbilityName("com.example.abilitydemo.ServiceAbility")
                .build();
        //设置操作
        intent.setOperation(operation);
        startAbility(intent);
    }

    /**
     * 停止本地服务 Ability
     */
    public void stopLocalServiceAbility() {
        Intent intent = new Intent();
        //构建操作方式
        Operation operation = new Intent.OperationBuilder()
                // 设备 id
                .withDeviceId("")
                // 应用的包名
                .withBundleName("com.example.abilitydemo")
                // 跳转目标的路径名 通常是包名+类名
                .withAbilityName("com.example.abilitydemo.ServiceAbility")
                .build();
        //设置操作
        intent.setOperation(operation);
        //停止服务
        stopAbility(intent);
    }

    /**
     * 连接本地服务 Ability
     */
    private void connectLocalServerAbility() {
        // 连接 Service
        Intent intent = new Intent();
        //构建操作方式
        Operation operation = new Intent.OperationBuilder()
                // 设备 id
                .withDeviceId("")
                // 应用的包名
                .withBundleName("com.example.abilitydemo")
                // 跳转目标的路径名 通常是包名+类名
                .withAbilityName("com.example.abilitydemo.ServiceAbility")
                .build();
        //设置操作
        intent.setOperation(operation);
        //连接到服务
        connectAbility(intent, connection);
    }
}
